import { dateUtil } from "/@/utils/dateUtil"
import { } from "/@/utils/index"
import type { Rule } from "/@/components/Form/src/types/form.ts"
export const rules = {
  rule(type, required) {
    if (type === "email") {
      return this.email(required)
    }
    if (type === "phone") {
      return this.phone(required)
    }
    if (type === "http") {
      return this.http(required)
    }
  },
  email(required) {
    return [
      {
        required: required ? required : false,
        validator: async (_rule, value) => {
          if (required == true && !value) {
            return Promise.reject("请输入邮箱!")
          }
          if (
            value &&
            !new RegExp(
              /^(([^<>()\[\]\\.,;:\s@"]+(\.[^<>()\[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/
            ).test(value)
          ) {
            return Promise.reject("请输入正确邮箱格式!")
          }
          return Promise.resolve()
        },
        trigger: "change",
      },
    ] as ArrayRule
  },
  phone(required) {
    return [
      {
        required: required,
        validator: async (_, value) => {
          if (required && !value) {
            return Promise.reject("请输入手机号码1!")
          }
          if (!new RegExp(/^1[3|4|5|7|8|9][0-9]\d{8}$/).test(value)) {
            return Promise.reject("手机号码格式有误")
          }
          return Promise.resolve()
        },
        trigger: "change",
      },
    ]
  },
  noIncludeChinese(required) : Rule[] {
    return [
      {
        required: required,
        validator: async (_, value) => {
          if (required && !value) {
            return Promise.reject("不能为空")
          }
          if (new RegExp('[\u4E00-\u9FA5\uFE30-\uFFA0]').test(value)) {
            return Promise.reject("不能包含中文")
          }
          return Promise.resolve()
        },
        trigger: "change",
      },
    ]
  },
  maxLength(required, maxLength): Rule[] {
    return [
      {
        required: required,
        validator: async (_, value) => {
          if (required && !value) {
            return Promise.reject("请输入内容")
          }
          if (value?.length > maxLength) {
            return Promise.reject(`长度超过 ${maxLength}`)
          }
          return Promise.resolve()
        },
        trigger: "change",
      },
    ]
  },
  minLength(required, minLength): Rule[] {
    return [
      {
        required: required,
        validator: async (_, value) => {
          if (required && !value) {
            return Promise.reject("请输入内容")
          }
          if (value.length < minLength) {
            return Promise.reject(`长度小于 ${minLength}`)
          }
          return Promise.resolve()
        },
        trigger: "change",
      },
    ]
  },
  http(required) {
    return [
      {
        required: required,
        validator: async (_, value) => {
          if (required && !value) {
            return Promise.reject("请输入正确地址!")
          }
          if (!new RegExp(/http(s)?:\/\/([\w-]+\.)+[\w-]+(\/[\w- .\/?%&=]*)?/).test(value)) {
            return Promise.reject("地址有误")
          }
          return Promise.resolve()
        },
        trigger: "change",
      },
    ]
  },
  startTime(endTime, required) {
    return [
      {
        required: required ? required : false,
        validator: (_, value) => {
          if (required && !value) {
            return Promise.reject("请选择开始时间")
          }
          if (endTime && value && dateUtil(endTime).isBefore(value)) {
            return Promise.reject("开始时间需小于结束时间")
          }
          return Promise.resolve()
        },
        trigger: "change",
      },
    ]
  },
  endTime(startTime, required) {
    return [
      {
        required: required ? required : false,
        validator: (_, value) => {
          if (required && !value) {
            return Promise.reject("请选择结束时间")
          }
          if (startTime && value && dateUtil(value).isBefore(startTime)) {
            return Promise.reject("结束时间需大于开始时间")
          }
          return Promise.resolve()
        },
        trigger: "change",
      },
    ]
  },
  confirmPassword(values, required) {
    return [
      {
        required: required ? required : false,
        validator: (_, value) => {
          if (!value) {
            return Promise.reject("密码不能为空")
          }
          if (value !== values.password) {
            return Promise.reject("两次输入的密码不一致!")
          }
          return Promise.resolve()
        },
      },
    ]
  },
  duplicateCheckRule(tableName, fieldName, model, schema, required?) {
    return [
      {
        validator: (_, value) => {
          if (!value && required) {
            return Promise.reject(`请输入${schema.label}`)
          }
          return new Promise<void>((resolve, reject) => {
            // duplicateCheck({
            //   tableName,
            //   fieldName,
            //   fieldVal: value,
            //   dataId: model.id,
            // })
            //   .then((res) => {
            //     res.success ? resolve() : reject(res.message || '校验失败');
            //   })
            //   .catch((err) => {
            //     reject(err.message || '验证失败');
            //   });
            return resolve()
          })
        },
      },
    ] as ArrayRule
  },
}

/**
 * 唯一校验函数，给原生<a-form>使用，vben的表单校验建议使用上述rules
 * @param tableName 表名
 * @param fieldName 字段名
 * @param fieldVal 字段值
 * @param dataId 数据ID
 */
export async function duplicateValidate(tableName, fieldName, fieldVal, dataId) {
  try {
    const params = {
      tableName,
      fieldName,
      fieldVal,
      dataId: dataId,
    }
    const res = await duplicateCheck(params)
    if (res.success) {
      return Promise.resolve()
    } else {
      return Promise.reject(res.message || "校验失败")
    }
  } catch (e) {
    return Promise.reject("校验失败,可能是断网等问题导致的校验失败")
  }
}
